package org.leetcode.easy.leetcode66;


import java.util.Arrays;

public class Solution
{
    public int[] plusOne(int[] digits) {


        int length = digits.length;

        return changeArray(digits,length,length);

    }

    private int[]  changeArray(int[] digits,int length,int max)
    {
        if (digits[length-1]+1>=10)
        {
            digits[length-1]=digits[length-1]-9;

            if (length-2<0)
            {
                digits= new int[max + 1];
                digits[0]=1;
                return digits;
            }
           digits= changeArray(digits,length-1,max);
        }else {
            digits[length-1]++;
        }

        return digits;
    }


    /**
     * 官方题解
     * @param digits
     * @return
     */
    public int[] plusOne2(int[] digits) {

        int length = digits.length;


        if (digits[length-1]!=9)
        {
            digits[length-1]++;

            return digits;
        }



        for (int i =length-1; i >=0 ; i--) {

            if (digits[i]!=9)
            {
                digits[i]++;
                break;
            }else {

                if (i==0)
                {
                    digits=new int[length+1];
                    digits[0]=1;
                    return digits;
                }

                digits[i]=0;
            }

        }


        return digits;
    }


        public static void main(String[] args) {
        int [] numbers = {9};

        Solution solution = new Solution();
        int[] plusOne = solution.plusOne2(numbers);

        System.out.println(Arrays.toString(plusOne));

    }


}
